package com.github.library.screenShot;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.WindowManager;
import android.widget.RelativeLayout;

/**
 * Created by Calvin.Wang on 12/3/2014.
 */
public class ClipImageLayout extends RelativeLayout {
    private ClipZoomImageView mZoomImageView;
    private ClipShadowView mClipShadowView;
    private ClipImageBorderView mClipImageView;
    private int mHorizontalPadding = 30;
    private Context mContext;

    public ClipImageLayout(Context context) {
        super(context);
        mContext = context;
    }

    public ClipImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public ClipImageLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    public void setHorizontalPadding(int mHorizontalPadding) {
        this.mHorizontalPadding = mHorizontalPadding;
    }

    public Bitmap clip() {
        return mZoomImageView.clip();
    }

    public void show(Context context, String imagePath) {
        mZoomImageView = new ClipZoomImageView(context);
        mClipShadowView = new ClipShadowView(context);
        mClipImageView = new ClipImageBorderView(context);

        android.view.ViewGroup.LayoutParams lp = new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);

        this.scaleImage(imagePath);

        this.addView(mZoomImageView, lp);
        this.addView(mClipShadowView, lp);
        this.addView(mClipImageView, lp);

        mHorizontalPadding = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mHorizontalPadding, getResources()
                        .getDisplayMetrics());
        mZoomImageView.setHorizontalPadding(mHorizontalPadding);
        mClipShadowView.setHorizontalPadding(mHorizontalPadding);
        mClipImageView.setHorizontalPadding(mHorizontalPadding);
    }

    private void scaleImage(String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //only read image information to memory
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int windowHeight = windowManager.getDefaultDisplay().getHeight();
        int windowWidth = windowManager.getDefaultDisplay().getWidth();
        int scaleX = imageWidth / windowWidth;
        int scaleY = imageHeight / windowHeight;
        int scale = 1;
        if (scaleX > scaleY && scaleY >= 1) {
            scale = scaleX;
        }

        if (scaleX < scaleY && scaleX >= 1) {
            scale = scaleY;
        }

        options.inJustDecodeBounds = false;
        options.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        mZoomImageView.setImageBitmap(bitmap);
    }
}
